package com.zhihuichongcheng.application.ui.cricle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.cricle.HealthBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.cricle.adapter.CricleHealthyAdapter;
import com.zhihuichongcheng.application.util.RecycleViewDivider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class CricleFoodActivity extends BaseActivity {
    private XRecyclerView mRcvFood;
    private CricleHealthyAdapter mAdapter = null;
    private List<HealthBean> mListData = new ArrayList<>();
    private String type;
    private String mTitle;
    private int mPageSize =10;
    private int mPage =1;
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle_food);
        initView();
        initData();
    }

    private void initView()
    {
        setStatusBarDarkMode();

        mRcvFood = (XRecyclerView)findViewById(R.id.rcv_food);
        mRcvFood.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CricleHealthyAdapter(mContext);
        mAdapter.setUserId(mUser.getUserId());
        mRcvFood.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.color_d8d8d8)));
        mRcvFood.setAdapter(mAdapter);
        mRcvFood.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
               mPage =1;
                loadData(mPage);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });
    }

    private void initData()
    {
        Intent intent = getIntent();
        if(intent!=null&&intent.hasExtra("title"))
        {
            mTitle = intent.getStringExtra("title");
        }
        if(intent!=null&&intent.hasExtra("type"))
        {
            type = intent.getStringExtra("type");
        }
        setTitle(mTitle);
        setImRight(R.drawable.ic_add_cricle_press, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AddCricleActivity.class);
            }
        });
        mAdapter.setData(mListData);
        mAdapter.notifyDataSetChanged();
        loadData(mPage);
    }

    /***
     * 加载每页的数据
     * @param page
     */
    private void loadData(final int page)
    {
        HashMap<String,String> param = new HashMap<>();
        param.put("token",mUser.getToken());
        param.put("jinbaoHomeCategoryId",type);
        param.put("pageRows",""+mPageSize);
        param.put("curPage",""+page);
        mManager.post(NetworkAPI.CricleList, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<HealthBean> list = GsonUtil.paraeFromStringToList(value,HealthBean.class);
                if(page ==1)
                {
                mAdapter.clear();
                }
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvFood.loadMoreComplete();
                mRcvFood.refreshComplete();
                if(list.size()<mPageSize)
                {
                    mRcvFood.setNoMore(true);
                }

            }

            @Override
            public void onFailure(String value) {

            }
        },param);
    }
}
